package
{
    import com.greensock.TweenMax;

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;

    [SWF(width="500", height="500", frameRate="36", backgroundColor="0xFFFFFF")]
    public class main extends Sprite
    {
        public function main()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            this.addEventListener(Event.ENTER_FRAME, enterFrameHanlder, false, 0, true);
        }

        private function enterFrameHanlder(event:Event):void
        {
            for(var i:int = 0; i < 4; i++)
            {
                var tempColor:uint = int(Math.random() * 16777215);

                var speed:Number = Number(Math.random() * 10);

                var size1:int = int(Math.random() * 10) + 1;
                var size2:int = size1 * 2;

                var formX:Number = 0;
                var formY:Number = 0;

                var toX:Number = 0;
                var toY:Number = 0;

                if(i % 2 == 0)
                {
                    formX = stage.stageWidth - (int(Math.random() * 200));
                    formY = stage.stageHeight - (int(Math.random() * 200));

                    toX = int(Math.random() * stage.stageWidth);
                    toY = int(Math.random() * stage.stageHeight);
                }
                else
                {
                    formX = -(int(Math.random() * 200));
                    formY = -(int(Math.random() * 200));

                    toX = stage.stageWidth - int(Math.random() * stage.stageWidth);
                    toY = stage.stageHeight - int(Math.random() * stage.stageHeight);
                }

                var box:Shape = new Shape;
                box.graphics.beginFill(tempColor);
                box.graphics.drawCircle(size1, size1, size1);
                box.graphics.endFill();

                var bitmapData:BitmapData = new BitmapData(size2, size2, true, tempColor);
                var bitamp:Bitmap = new Bitmap(bitmapData);
                bitamp.x = formX;
                bitamp.y = formY;
                bitamp.alpha = Math.random();
                bitmapData.draw(box);
                this.addChild(bitamp);

                box = null;

                TweenMax.to(bitamp, 3, {alpha: 1, x: toX, y: toY, onComplete: tweenOnComplete1, onCompleteParams: [bitamp]});
            }
        }

        private function tweenOnComplete1(bitamp:Bitmap):void
        {
            TweenMax.to(bitamp, 1, {alpha: 0, onComplete: tweenOnComplete2, onCompleteParams: [bitamp]});
        }

        private function tweenOnComplete2(bitmap:Bitmap):void
        {
            this.removeChild(bitmap);
        }
    }
}
